#-*- coding:utf-8 -*-
# Author: Liu Huan
# Mail: liuhuan@mail.las.ac.cn
# Datetime: 2019/7/31 16:06


import pandas as pd
import re


if __name__ == '__main__':


    # df_dict = {'id':[],'abstract':[]}
    # # 过滤一些

    # df = pd.read_csv('doaj_structured_new_new.csv')
    # for i in range(len(df)):
    #     abs = df.iloc[i]['abstract']
    #     if 'The aim: ' in abs or 'Study aim: ' in abs or '&#' in abs or '&lt' in abs or '&gt' in abs or '   ' in abs:
    #         continue
    #     elif abs.startswith('Background: ') and ('Aims: ' in abs or 'Objectives' in abs or 'Aim: ' in abs or 'Objective' in abs ):
    #         df_dict['id'].append(i)
    #         df_dict['abstract'].append(abs)
    #     elif abs.startswith('Context: ') and ('Aims: ' in abs or 'Objectives' in abs or 'Aim: ' in abs or 'Objective' in abs ):
    #         df_dict['id'].append(i)
    #         df_dict['abstract'].append(abs)

    #     elif abs.startswith('Objective: ') or abs.startswith('Aims: ') or abs.startswith('Objectives: '):
    #         df_dict['id'].append(i)
    #         df_dict['abstract'].append(abs)
            
    #     # elif abs.startswith('Objective: ') and 'Mehtods: ' in abs:
    #     #     df_dict['id'].append(i)
    #     #     df_dict['abstract'].append(abs)
    #     # df_dict['id'].append(i)
    #     # df_dict['abstract'].append(abs)
        
    # df = pd.DataFrame(df_dict)

    # df.to_csv('doaj_structured_clean.csv')
    # exit()




    df = pd.read_csv('doaj_structured_clean.csv')



    df_dict = {'id':[],'move':[],'text':[]}

    move_type_merge = {
        'background:':'BACKGROUND',
        'context:':'BACKGROUND',
        'aims:':'OBJECTIVE',
        'aim:':'OBJECTIVE',
        'objective:':'OBJECTIVE',
        'objectives:':'OBJECTIVE',
        'methods:':'METHODS',
        'method:':'METHODS',
        'results:':'RESULTS',
        'result:':'RESULTS',
        'conclusions:':'CONCLUSIONS',
        'conclusion:':'CONCLUSIONS',
    }

    for i in range(len(df)):
        id_ = df.iloc[i]['id']
        abst = df.iloc[i]['abstract']

        if 'Funding:' in abst or 'Trial registration:' in abst or 'Design:' in abst:
            continue

        if 'Context:' in abst and ('Aims:' in abst or 'Aim:' in abst):

            ## 第一种模式
            moves = []
            for m in ['Context:','Aims:','Aim:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"Context:|Aims?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            try:
                assert len(moves) == len(abst_split)
            except:
                print(i)
                print(abst)
                print(abst_split)
                print(moves)
                exit()
                
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'Context:' in abst and ('Objective:' in abst or 'Objectives:' in abst):

            ## 第二种模式
            moves = []
            for m in ['Context:','Objective:','Objectives:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)
        
            abst_split = re.split(r"Context:|Objectives?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)
        
        elif 'Background:' in abst and ('Aims:' in abst or 'Aim:' in abst):

            ## 第三种模式
            moves = []
            for m in ['Background:','Aims:','Aim:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"Background:|Aims?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            try:
                assert len(moves) == len(abst_split)
            except:
                print(i)
                print(abst)
                print(abst_split)
                print(moves)
                exit()
                
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'Background:' in abst and ('Objective:' in abst or 'Objectives:' in abst):

            ## 第四种模式
            moves = []
            for m in ['Background:','Objective:','Objectives:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)
        
            abst_split = re.split(r"Background:|Objectives?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            
            try:
                assert len(moves) == len(abst_split)
            except:
                print(i)
                print(abst)
                print(abst_split)
                print(moves)
                exit()
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)


        elif abst.startswith('Objective'):
            # 第五种模式
            moves = []
            for m in ['Objective:','Objectives:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)
        
            abst_split = re.split(r"Objectives?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            try:
                assert len(moves) == len(abst_split)
            except:
                print(i)
                print(abst)
                print(abst_split)
                print(moves)
                exit()
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)
        

        elif abst.startswith('Aim'):
            # 第六种模式
            moves = []
            for m in ['Aim:','Aims:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)
        
            abst_split = re.split(r"Aims?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)



    df_result = pd.DataFrame(df_dict)
    df_result.to_csv('result_move.csv')